package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.charshop;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.ImageCache;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Role;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;

class CharListRenderer extends JLabel implements ListCellRenderer {
	
	ImageCache imgCache;
	
    public CharListRenderer(ImageCache imgCache) {
    	this.imgCache = imgCache;
        setOpaque(true);
    }

    public Component getListCellRendererComponent(JList list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus) {

    
        Role base = (Role) value;
        Color background;
        Color foreground;
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setVerticalTextPosition(JLabel.BOTTOM);
        this.setText(base.getName().toUpperCase());
        this.setIcon(new ImageIcon(imgCache.getImage("cbvapps/textures/rolesimgs/"+base.getName()+"/avatar.png")));
        // check if this cell represents the current DnD drop location
        JList.DropLocation dropLocation = list.getDropLocation();
        if (dropLocation != null
                && !dropLocation.isInsert()
                && dropLocation.getIndex() == index) {

            background = Color.BLUE;
            foreground = Color.WHITE;

        // check if this cell is selected
        } else if (isSelected) {
            background = Color.RED;
            foreground = Color.WHITE;

        // unselected, and not the DnD drop location
        } else {
            background = Color.WHITE;
            foreground = Color.BLACK;
        };

        setBackground(background);
        setForeground(foreground);

        return this;
    }
}
